<?php

namespace App\Entities;
use App\Contracts\GeneratesUniqueHash;
use App\Contracts\HasIdColumn;
use App\Entities\Base\AbstractEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Illuminate\Support\Collection;

/**
 * App\Entities\File
 *
 * @ORM\Entity(repositoryClass="App\Repositories\ExploitRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Exploit extends Base\Exploit implements HasIdColumn, GeneratesUniqueHash
{
    /**
     * @ORM\ManyToMany(targetEntity="Vulnerability", mappedBy="exploits")
     */
    protected $vulnerabilities;

    /**
     * Exploit constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->vulnerabilities = new ArrayCollection();
    }

    /**
     * @param Vulnerability $vulnerability
     * @return $this
     */
    public function addVulnerability(Vulnerability $vulnerability)
    {
        $this->vulnerabilities[] = $vulnerability;

        return $this;
    }

    /**
     * @param Vulnerability $vulnerability
     * @return $this
     */
    public function removeVulnerability(Vulnerability $vulnerability)
    {
        $this->vulnerabilities->removeElement($vulnerability);

        return $this;
    }

    /**
     * @inheritdoc
     * @return bool
     */
    public function hasMinimumRequiredPropertiesSet(): bool
    {
        return isset($this->title) && isset($this->skill_level);
    }

    /**
     * @return ArrayCollection
     */
    public function getVulnerabilities()
    {
        return $this->vulnerabilities;
    }

    /**
     * Get a hash value of all the property values whose combination creates a unique key
     *
     * @return string
     */
    public function getHash(): string
    {
        return AbstractEntity::generateUniqueHash($this->getUniqueKeyColumns());
    }

    /**
     * @inheritdoc
     * @return Collection
     */
    public function getUniqueKeyColumns(): Collection
    {
        return collect([
            parent::TITLE         => $this->title,
            parent::URL_REFERENCE => $this->url_reference,
        ]);
    }
}